package com.zzptc.zzsgz.bdsjws.fragments;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.AddContactsActivity;
import com.zzptc.zzsgz.bdsjws.activities.OneKeySOSActivity;
import com.zzptc.zzsgz.bdsjws.adapter.SOSAdapter;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragmentbase;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2017/5/27.
 */

public class SecondFragment extends BaseFragmentbase {

    @BindView(R.id.edAdd)
    EditText edAdd;
    @BindView(R.id.img_add)
    ImageView imgAdd;
    @BindView(R.id.btn_yes)
    Button btnYes;
    @BindView(R.id.rv_sos_tel)
    RecyclerView rvSostel;
    SOSAdapter sosAdapter;

    @Override
    public void initView() {
        edAdd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                imgAdd.setVisibility(View.GONE);
                btnYes.setVisibility(View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public int getXML() {
        return R.layout.second_fragment;
    }


    @OnClick(R.id.img_add)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), AddContactsActivity.class));
        getActivity().findViewById(R.id.tv_count);
    }

    @OnClick(R.id.btn_second)
    public void toThirdFragment(){
        //Toast.makeText(getActivity(), "7777777", Toast.LENGTH_SHORT).show();
        if (MyApplication.getSOSList().size()==0){
            Toast.makeText(getActivity(), "请选择求救联系人", Toast.LENGTH_SHORT).show();
        }else {
            //跳转
            if (getActivity() instanceof OneKeySOSActivity){
                ((OneKeySOSActivity)getActivity()).toThirdFragment();
            }
        }
    }
    @OnClick(R.id.btn_yes)
    public void onYesClicked() {
        //先判断电话号码的长度   并且要获取电话号码
        String tel=edAdd.getText().toString().trim();
        if (tel.length()==11&&tel.matches("^1[3578]\\d{9}$")){
            List<Contact> listSos= MyApplication.getSOSList();
            Contact contact=new Contact();
            contact.setPhonenum(tel);
            if (listSos.size()<3){
                listSos.add(contact);
            }else {
                Toast.makeText(getActivity(), "已有三人", Toast.LENGTH_SHORT).show();
            }

            //适配器（重新构造一个 项） 有数据源  布局管理器
            sosAdapter=new SOSAdapter(listSos);
            rvSostel.setLayoutManager(new LinearLayoutManager(getActivity()));
            rvSostel.setAdapter(sosAdapter);

            //只能控制三人
        }
        //判断电话号码的长度
        edAdd.setText("");
        imgAdd.setVisibility(View.VISIBLE);
        btnYes.setVisibility(View.GONE);
    }

    /**
     * 碎片出现的时候就会调用
     */
    @Override
    public void onResume() {
        super.onResume();
        //Toast.makeText(getActivity(), "========", Toast.LENGTH_SHORT).show();
        if(sosAdapter!=null) {
            sosAdapter.notifyDataSetChanged();
        }
    }
}
